<?php
class ml_model_dbFollow extends Lib_datamodel_db
{
    function __construct() {
        $db_config = ml_factory::load_standard_conf ( 'db' );
        parent::__construct ( 'follow', $db_config ['follow'] );
    }
    
    /**
     * 增关注
     * @param  $uid        当前登录者uid
     * @param  $follow_uid     关注的uid
                 
     */
    function addFollow($uid,$follow_uid) {
        if(!$this->init_db($uid))
            return false;
        
        $data = array(
                'uid'=>$uid,
                'follow_uid'=>$follow_uid,
                'cip'=>Tool_ip::get_real_ip()
                );
        return $this->insert($data);        
    }
    
    /**
     * 取消关注
     * @param  $uid        当前登录者uid
     * @param  $follow_uid     被取消关注的uid    
     */
    function delFollow($uid,$follow_uid) {
        if(!isset($uid)) {
            return false;
        }
        if(!$this->init_db($uid))
            return false;
        $sql = 'DELETE FROM '.$this->table.' WHERE uid = '.$uid.' AND follow_uid = '.$follow_uid;        
        
        return $this->query($sql);
    }
    
    /**
     * 是否关注过
     * @param  $uid        当前登录者uid
     * @param  $follow_uid     关注的uid
         
     */
    function isFollow($uid,$follow_uid) {
        if(!isset($uid)) {
            return false;
        }
        if(!$this->init_db($uid,self::DB_SLAVE))
            return false;
        
        $where = 'uid = '.$uid.' and follow_uid = '.$follow_uid;                            
        
        return $this->fetch_count($where);
    }
    
    /**
     * 获取关注列表
     * @param  $uid        当前登录者uid
     */
    function getList($uid,$begin,$end) {
        if(!isset($uid)) {
            return false;
        }
        
        if(!$this->init_db($uid,self::DB_SLAVE))
            return false;
        $sql = 'SELECT follow_uid FROM '.$this->table.' where `uid` ='.$uid .' order by ctime desc limit '.$begin.','.$end;
        return $this->query($sql,true);    
    }
    
    function getListByFollowUid($uid , $aUid)
    {
        if(!isset($uid)) {
            return false;
        }
        if(!$this->init_db($uid,self::DB_SLAVE))
            return false;
        $sql = 'SELECT * FROM '.$this->table.' where `uid` ='.$uid .' and follow_uid in ('.implode(',' , $aUid).')';
        
        return $this->fetch($sql,true);
    }
    

    /**
     * 批量关注很多人
     * @param int $uid
     * @param array $aUid
     */
    
    function addFollows($uid,$aUid) {
        if(!$this->init_db($uid))
            return false;
        $ip = Tool_ip::get_real_ip();
        if(is_array($aUid) && count($aUid) > 0){
            $value = array();
            foreach($aUid as $k => $v) {
                $value[] = '('.$uid.', '.$v.', \''.$ip.'\')';
            }
            $val = implode(',', $value);
            $sql = 'INSERT INTO  '.$this->table.' (`uid`,`follow_uid`,`cip`)  VALUES  '.$val ;
        
            return $this->query($sql,true);
        }
    }
    
    public function countFollows($uid) {
        if(!isset($uid)) {
            return false;
        }
        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;

        $where = " uid= {$uid} ";
        $rs = $this->fetch_count($where);
        if (!$rs) return false;
        return true;
    }    

}
